package com.tzy.for10evalue.utils;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @author tzy
 * @date 2021/12/16 15:58
 */
public class WaterMarkUtil {

    /**
     * @param pressText            水印文本
     * @param targetImgInputStream 目标图片
     * @param fileName             源文件名称
     * @param response
     */
    public static void pressText(String pressText, InputStream targetImgInputStream, String fileName, HttpServletResponse response, String imgType) {

        try {
            Image srcImg = ImageIO.read(targetImgInputStream);
            int srcImgWidth = srcImg.getWidth(null);
            int srcImgHeight = srcImg.getHeight(null);
            Integer degree = -45;//设置水印文字的旋转角度
            float alpha = 0.5f;//设置水印透明度 默认为1.0  值越小颜色越浅
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth,
                    srcImgHeight,
                    BufferedImage.TYPE_INT_RGB);
            //获取 Graphics2D 对象
            Graphics2D g = bufImg.createGraphics();
            //设置绘图区域
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.rotate(Math.toRadians(degree), (double) bufImg.getWidth(), (double) bufImg.getHeight());//设置水印旋转
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));//设置水印文字透明度

            //设置字体 服务器可能没这个字体类型 所以暂时先置空
            Font font = new Font("宋体", Font.PLAIN, 30);
            g.setFont(font);

            // 根据图片的背景设置水印颜色
            g.setColor(new Color(138, 123, 92, 250));
            //获取文字长度
            int width = g.getFontMetrics(
                    g.getFont()).charsWidth(pressText.toCharArray(),
                    0,
                    pressText.length());
            //文字水印的宽
            int rowsNumber = srcImgHeight / width + srcImgHeight % width;// 图片的高  除以  文字水印的宽  打印的行数(以文字水印的宽为间隔)
            int columnsNumber = srcImgWidth / width + srcImgWidth % width;//图片的宽 除以 文字水印的宽  每行打印的列数(以文字水印的宽为间隔)
            //防止图片太小而文字水印太长，所以至少打印一次
            if (rowsNumber < 1) {
                rowsNumber = 1;
            }
            if (columnsNumber < 1) {
                columnsNumber = 1;
            }
            for (int j = 0; j < rowsNumber; j++) {
                for (int i = 0; i < columnsNumber; i++) {
                    g.drawString(pressText, i * width + j * width, -i * width + j * width);//画出水印,并设置水印位置
                }
            }
            g.dispose();

            //输出流
            OutputStream out = null;
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + " ");
            out = response.getOutputStream();
            //输出图片类型
            List<String> picSuffixList = Arrays.asList("png", "jpeg", "jpg", "bmp", "gif");
            if(picSuffixList.contains(imgType)){
                response.setContentType("img/"+imgType);
                ImageIO.write(bufImg, imgType, out);
            }else{
                out.flush();
                out.close();
                throw new RuntimeException("不支持的图片类型");
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
